package com.wn.base.algorithm;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @FileName: Demo
 * @Description: TODO
 * @Author: MaWenHao
 * @Created: 2020/11/18 - 14:16
 * @Version: V1.0
 * @Company: 东方微银科技（北京）有限公司
 */
public class Demo {

    public static void main(String[] args) {
        // 原始数据集
        List<Entity> entities = Demo.buildEntities();
        // 条件属性的优势集
        Set<String> rA = new HashSet<>(8);
        // 包含决策属性的优势集
        Set<String> rD = new HashSet<>(8);

        for (Entity x : entities) {
            for (Entity y : entities) {
                // 获取所有条件属性
                List<ConditionField<Integer>> conditionFieldX = x.getConditionFields();
                List<ConditionField<Integer>> conditionFieldY = y.getConditionFields();
                // x和y组合的唯一id
                String serial = x.getId() + y.getId();
                // 判断y是不是x的条件优势对象
                boolean isCondAdvantage = Demo.isCondAdvantage(conditionFieldX, conditionFieldY);
                if (isCondAdvantage) {
                    rA.add(serial);
                }
                // 判断y是不是x的决策优势对象
                boolean isDecisionAdvantage = Demo.isDecisionAdvantage(x, y);
                if (isCondAdvantage && isDecisionAdvantage) {
                    rD.add(serial);
                }
            }
        }

        // 计算数据集是否协调
        boolean isCoordinate = Demo.isCoordinate(rA, rD);
    }

    /**
     * 计算set1中的所有元素是否都在set2中
     * @param set1
     * @param set2
     * @return
     */
    private static boolean isCoordinate(Set<String> set1, Set<String> set2) {
        for (String ra : set1) {
            if (!set2.contains(ra)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断y是不是x的决策优势对象
     *
     * @param x
     * @param y
     * @return
     */
    private static boolean isDecisionAdvantage(Entity x, Entity y) {
        Integer decisionX = x.getDecision();
        Integer decisionY = y.getDecision();
        return decisionY >= decisionX;
    }

    /**
     * 判断y是不是x的条件优势对象
     *
     * @param conditionFieldX x的条件集
     * @param conditionFieldY y的条件集
     * @return
     */
    private static boolean isCondAdvantage(List<ConditionField<Integer>> conditionFieldX, List<ConditionField<Integer>> conditionFieldY) {
        for (int i = 0; i < conditionFieldX.size(); i++) {
            ConditionField<Integer> condX = conditionFieldX.get(i);
            Integer lastX = condX.getLast();
            ConditionField<Integer> condY = conditionFieldY.get(i);
            Integer lastY = condY.getLast();
            if (lastX > lastY) {
                return false;
            }
        }
        return true;
    }

    /**
     * 初始化数据集,将原始数据转换为标准结果
     *
     * @return
     */
    private static List<Entity> buildEntities() {
        return null;
    }
}
